<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1.创建promise对象
        const p = new Promise((resolve,reject)=>{
            // 2.执行XHR异步代码
            const xhr = new XMLHttpRequest()
            xhr.open('GET','http://hmajax.itheima.net/api/province')
            xhr.addEventListener('loading',()=>{
                if(xhr.status>=200&&xhr.status<300){
                    resolve(JSON.parse(xhr.response))
                }else{
                    reject(new Error(xhr.response))
                }
            })
            xhr.send()
        })
        // 3.关联成功或失败函数
        p.then(result=>{
            console.log(result)
        }).catch(error=>{
            console.dir(error.message)
        })
    </script>
</body>
</html>